package com.gwtgeo.ui.client.mvp.adapter;

import java.util.Date;

import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasValue;
import com.smartgwt.client.widgets.form.fields.FormItem;

public class HasValueFormItemAdapter<T> implements HasValue<T> {

	private FormItem item;

	public HasValueFormItemAdapter(FormItem item) {
		super();
		this.item = item;
	}

	@Override
	@SuppressWarnings("unchecked")
	public T getValue() {
		return (T) item.getValue();
	}

	@Override
	public void setValue(T value, boolean fireEvents) {
	}

	@Override
	public void setValue(T value) {
		if (value instanceof String) {
			item.setValue((String) value);
		} else if (value instanceof Integer) {
			item.setValue(((Integer) value).intValue());
		} else if (value instanceof Double) {
			item.setValue(((Double) value).doubleValue());
		} else if (value instanceof Float) {
			item.setValue(((Float) value).floatValue());
		} else if (value instanceof Boolean) {
			item.setValue(((Boolean) value).booleanValue());
		} else if (value instanceof Date) {
			item.setValue((Date) value);
		}
	}

	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<T> handler) {
		return null;
	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
	}

}
